﻿using System.Windows.Forms;

namespace GeoSketch2.Core
{
    /// <summary>
    /// The selection tool, check is any object is near the clic and puts it in the selected object list
    /// </summary>
    public class TSelection:BaseTool
    {
        #region Constructor
        /// <summary>
        /// The selection tool
        /// </summary>
        /// <param name="Viewport">The viewport</param> 
        public TSelection(BaseViewport Viewport)
            : base(Viewport,"Selection")
        {
        } 
        #endregion

        #region Inputs
        /// <summary>
        /// The method called when the mouse button is down
        /// </summary>
        public override void MouseDown(MouseEventArgs MouseArgs)
        {
            double is_near_distance_object_coordinates = Viewport.Object2ImageTransformation.InverseTransform(Vars.Setup.Tools.TSelection.IsNearDistance);
            Point2D64D clic_object_coordinates = Viewport.Object2ImageTransformation.InverseTransform(MouseArgs.Location);

            foreach (BaseDrawableObject obj in Viewport.GeoObjects)
            {
                if (obj.Layer.Visible)
                {
                    if (obj.IsNear(clic_object_coordinates, is_near_distance_object_coordinates))
                    {
                        if (obj.IsSelected)
                        {
                            obj.IsSelected = false;
                            Viewport.SelectedObjects.Remove(obj);
                        }
                        else
                        {
                            obj.IsSelected = true;
                            Viewport.SelectedObjects.Add(obj);
                        }
                        break;
                    }
                }
            }
            RefreshViewport();
        }
        #endregion

        #region Methods
        /// <summary>
        /// The method to be used to send the first message to the user, just after beeing selected
        /// </summary>
        public override void SendFirstMessage()
        {
            SendMessage("Click an object to select it");
        } 
        #endregion
    }
}
